package com.backstage.interceptor;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

public class ChatInterceptor extends HandlerInterceptorAdapter {
    private List<String> IGNORE_CHAT_URI;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取URI后缀
        String requestUri = request.getServletPath();

        //if(requestUri.equalsIgnoreCase("/chat/"))    { return true; }

        //过滤不需要拦截的地址
        for (String uri : IGNORE_CHAT_URI) {
            if (requestUri.startsWith(uri)) {
                return true;
            }
        }
        HttpSession session = request.getSession();
        if(session.getAttribute("SESSION_USERID") != null && Integer.parseInt(session.getAttribute("SESSION_USERID").toString()) > 0){ //chat_user 用户Id
            return true;
        }else{
            response.sendRedirect("/chat/login");
            return false;
        }
        //return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    public List<String> getIGNORE_CHAT_URI() {
        return IGNORE_CHAT_URI;
    }

    public void setIGNORE_CHAT_URI(List<String> IGNORE_CHAT_URI) {
        this.IGNORE_CHAT_URI = IGNORE_CHAT_URI;
    }
}
